package the.workshop.milktea.interfaces.controller;

import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.wisteria.framework.web.APIResult;
import org.wisteria.framework.web.QueryResult;
import the.broccoli.auth.UserContext;
import the.workshop.milktea.interfaces.req.CategoryPageReq;
import the.workshop.milktea.interfaces.req.ShopAddReq;
import the.workshop.milktea.interfaces.req.ShopModifyReq;
import the.workshop.milktea.interfaces.req.ShopRemoveReq;
import the.workshop.milktea.interfaces.resp.CategoryPageResp;
import the.workshop.milktea.interfaces.resp.ShopGetResp;
import the.workshop.milktea.service.TeaShopService;
import the.workshop.milktea.service.dto.ShopAddDTO;
import the.workshop.milktea.service.dto.ShopGetDTO;
import the.workshop.milktea.service.dto.ShopModifyDTO;
import the.workshop.milktea.service.dto.ShopPageDTO;

import javax.annotation.Resource;

/**
 * <p>
 * 种类 前端控制器
 * </p>
 *
 * @author huamu
 * @since 2023-04-01
 */
@Api(tags = "Tea")
@RestController
@RequestMapping("/milktea/shop")
public class TeaShopController {

    @Resource
    private TeaShopService teaShopService;

    @PostMapping("/add")
    public APIResult<Boolean> add(@RequestBody ShopAddReq req) {
        ShopAddDTO dto = new ShopAddDTO();
        BeanUtils.copyProperties(req, dto);
        dto.setOperatorId(UserContext.userId());
        dto.setOperatorName(UserContext.userName());
        return APIResult.success(teaShopService.add(dto));
    }

    @PostMapping("/modify")
    public APIResult<Boolean> modify(@RequestBody ShopModifyReq req) {
        ShopModifyDTO dto = new ShopModifyDTO();
        BeanUtils.copyProperties(req, dto);
        dto.setOperatorId(UserContext.userId());
        dto.setOperatorName(UserContext.userName());
        return APIResult.success(teaShopService.modify(dto));
    }

    @PostMapping("/remove")
    public APIResult<Boolean> remove(@RequestBody ShopRemoveReq req) {
        teaShopService.removeByIds(req.getIds(), UserContext.userId(), UserContext.userName());
        return APIResult.success();
    }

    @GetMapping("/page")
    public APIResult<QueryResult<CategoryPageResp>> page(CategoryPageReq req) {
        ShopPageDTO dto  = new ShopPageDTO();
        BeanUtils.copyProperties(req, dto);
        dto.setPageNum(req.getPageNum());
        dto.setPageSize(req.getPageSize());
        QueryResult queryResult = teaShopService.pageList(dto, req.getPageNum(), req.getPageSize());
        return APIResult.success(queryResult);
    }

    @GetMapping("/{id}")
    public APIResult<ShopGetResp> get(@PathVariable(value = "id") Integer id) {
        ShopGetResp resp = null;
        ShopGetDTO dto = teaShopService.get(id);
        if (dto != null) {
            resp = new ShopGetResp();
            BeanUtils.copyProperties(dto, resp);
        }
        return APIResult.success(resp);
    }
}
